package cs342.uwosh.edu.QuizApp;

import java.util.ArrayList;

import cs342.uwosh.edu.R;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter{
	
	private ArrayList<String> courseList;
	private ArrayList<ArrayList<String>> quizList;
	private Context context;


	public ExpandableListAdapter(Context context, ArrayList<String> courseList, ArrayList<ArrayList<String>> quizList){
		this.context = context;

		this.courseList = courseList;
		this.quizList = quizList;
	}
	public Object getChild(int groupPosition, int childPosition) {
		return quizList.get(groupPosition).get(childPosition);
	}
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		String quizName =  (String) getChild(groupPosition, childPosition);
		if (convertView == null){
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.child_layout, null);
		}
		TextView textView = (TextView) convertView.findViewById(R.id.tvChild);
		textView.setText(quizName);
		return convertView;
	}


	public int getChildrenCount(int groupPosition) {
		return quizList.get(groupPosition).size();
	}


	public Object getGroup(int groupPosition) {
		return courseList.get(groupPosition);
	}
	

	public String getCourseName (int groupPos){
		return courseList.get(groupPos);
	}


	public int getGroupCount() {
		return courseList.size();
	}

	
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String group = (String)getGroup(groupPosition);
		if (convertView == null){
			LayoutInflater infalInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.group_layout, null);
		}
		TextView textView = (TextView)convertView.findViewById(R.id.tvGroup);
		textView.setText(group);
		return convertView;
	}

	public boolean hasStableIds() {
		return true;
	}
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	public void addCourse(String newCourse){
		courseList.add(newCourse);
		quizList.add(new ArrayList<String>());
	}
	
	public void addQuiz(int groupPosition, String quizName){
		quizList.get(groupPosition).add(quizName);
	}
	
	public ArrayList<String> getAllGroups(){
		return courseList;
	}
	
	public void editContact(String quizName, int groupPosition, int childPosition){
		quizList.get(groupPosition).remove(childPosition);
		quizList.get(groupPosition).add(quizName);
	}
	
	public void deleteGroup(int groupPosition){
		courseList.remove(groupPosition);
		quizList.remove(groupPosition);
	}
	
	public void deleteContact(int groupPosition, int childPosition){
		quizList.get(groupPosition).remove(childPosition);
	}
}
